/*
 * @Date: 2025-04-18 16:42:58
 * @LastEditors: Csir 2215995096@qq.com
 * @LastEditTime: 2025-08-14 17:27:31
 * @FilePath: \YUJIN-O3\src\main.js
 */
import "babel-polyfill";
import "classlist-polyfill";
import Vue from "vue";
import axios from "./router/axios";
import VueAxios from "vue-axios";
import App from "./App";
import zhLocale from "./lang/zh";
import "./permission"; // 权限
import "./error"; // 日志
import "./cache"; //页面缓冲
import router from "./router/router";
import store from "./store";
import { loadStyle, downBlobFile } from "./util";
import { validatenull } from "./util/validate";
import { base64Encrypt } from "./util/encryption";
import * as urls from "@/config/env";
import { iconfontUrl } from "@/config/env";
import * as filters from "./filters"; // 全局filter
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "./styles/common.scss";

import AvueFormDesign from "pig-avue-form-design";
import basicContainer from "./components/basic-container/main";
// 字典数据组件
import DictResolver from "@/components/DictResolver";
// 字典标签组件
import DictTag from "@/components/DictTag";

// import WujieVue2 from "wujie-vue2";

// import Antd from "ant-design-vue";
// import "ant-design-vue/dist/antd.css";

// import VScaleScreen from "v-scale-screen";

// import FitScreen from 'fit-screen'

// 挂载常用全局方法，import 引入
Vue.prototype.validatenull = validatenull;
Vue.prototype.downBlobFile = downBlobFile;
Vue.prototype.base64Encrypt = base64Encrypt;

DictResolver.install();

// 插件 json 展示
Vue.use(router);
// 表单设计器
Vue.use(AvueFormDesign);

window.axios = axios;
Vue.use(VueAxios, axios);

// Vue.use(WujieVue2);

Vue.use(ElementUI, {
  size: "small",
  menuType: "text",
});

// Vue.use(Antd);
// Vue.use(VScaleScreen)
// Vue.use(FitScreen)
// eslint-disable-next-line
Vue.use(AVUE, {
  locale: zhLocale,
  size: "small",
  menuType: "text",
});
// 注册全局容器
Vue.component("basicContainer", basicContainer);
Vue.component("DictTag", DictTag);

// 加载相关url地址
Object.keys(urls).forEach((key) => {
  Vue.prototype[key] = urls[key];
});

// 加载过滤器
Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key]);
});

// 动态加载阿里云字体库
iconfontUrl.forEach((ele) => {
  loadStyle(ele);
});

Vue.config.productionTip = false;

if (window.$wujie) {
  // 添加加载状态提示
  let loadingInstance = ElementUI.Loading.service({
    lock: true,
    text: "资源加载中...",
    spinner: "el-icon-loading",
    background: "#fff",
  });

  (() => {
    try {
      const { token, url } = window.$wujie.props.info;
      console.log("子应用token", token);
      console.log("子应用url", url);

      if (token) {
        localStorage.setItem("infomng-access_token", token);
        store.commit("SET_ACCESS_TOKEN", token);

        store.dispatch("GetUserInfo");

        store
          .dispatch("GetMenu", {
            type: true,
            id: 1,
          })
          .then((res) => {
            setTimeout(() => {
              // 确保路由就绪后再跳转
              router.onReady(() => {
                console.log("start", url);
                router
                  .push(url)
                  .catch((error) => {
                    console.error("路由跳转失败:", error);
                    // router.push("/error/404");
                  })
                  .finally(() => {
                    loadingInstance.close(); // 关闭加载提示
                  });
              });
            }, 500);
          });
      } else {
        localStorage.removeItem("infomng-access_token");
        router.push("/login");
      }

      window.$wujie.bus.$on("update-props", (newProps) => {
        console.log("收到新props", newProps);
        const { url } = newProps.info;
        // 确保路由就绪后再跳转
        router.onReady(() => {
          console.log("reflush", url);
          router
            .push(url)
            .catch((error) => {
              console.error("路由跳转失败1:", error);
              // router.push("/error/404");
            })
            .finally(() => {
              loadingInstance.close(); // 关闭加载提示
            });
        });
      });

      // 监听主应用传递的新 token
      window.$wujie.bus.$on("token:refresh", (newToken) => {
        console.log("子应用刷新token", newToken);
        localStorage.setItem(
          "infomng-access_token-1",
          JSON.stringify(newToken)
        );
        store.commit("SET_ACCESS_TOKEN", newToken.accessToken);
      });

      // 监听主应用登出事件
      window.$wujie.bus.$on("token:logout", () => {
        localStorage.removeItem("infomng-access_token");
        router.push("/login");
      });
    } catch (error) {
      console.error("初始化失败:", error);
      loadingInstance.close();
      router.push("/error/500");
    }
  })();
}

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");

// 修改原来样式
import "./styles/revamp.scss";
